<template>
  <div>
    <!-- Breadcrumb Navigation area -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">Home</el-breadcrumb-item>
      <el-breadcrumb-item>Goods</el-breadcrumb-item>
      <el-breadcrumb-item>Gategories</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card view area -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">Add Category</el-button>
        </el-col>
      </el-row>
      <!-- cate(Category) list -->
      <tree-table
        class="tree_table"
        :data="cateList"
        :columns="columns"
        show-index
        index-text="#"
        :selection-type="false"
        :expand-type="false"
        :border="true"
        :show-row-hover="false"
        stripe
      >
        <!-- Category is deleted -->
        <template slot="isDeleted" slot-scope="sp">
          <i v-if="!sp.row.cat_deleted" class="el-icon-success" style="color: lightgreen"></i>
          <i v-if="sp.row.cat_deleted" class="el-icon-error" style="color: red"></i>
        </template>
        <!-- Category sort level -->
        <template slot="order" slot-scope="sp">
          <el-tag v-if="sp.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag v-if="sp.row.cat_level === 1" type="success" size="mini">二级</el-tag>
          <el-tag v-if="sp.row.cat_level === 2" type="warning" size="mini">三级</el-tag>
        </template>
        <!-- Operating -->
        <template slot="operating">
          <el-button type="primary" icon="el-icon-search" size="mini">Edit</el-button>
          <el-button type="danger" icon="el-icon-search" size="mini">Delete</el-button>
        </template>
      </tree-table>
      <!-- paging -->
      <!-- paging -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 10, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- add cate dialog -->
    <el-dialog
      title="Add Category"
      :visible.sync="addCateDialogVisible"
      width="47%"
      @close="closeAddCateDialog"
    >
      <!-- Add cate form -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="139px"
      >
        <el-form-item label="Category Name" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="Parent Category">
          <!-- options: data sourse -->
          <!-- props: config cascader -->
          <el-cascader
            :options="parentCateList"
            :props="cascaderProps"
            v-model="selectedkeys"
            @change="parentCateChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="addCate">I Sure</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      /* params for get user list data */
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 3
      },
      cateList: [],
      total: 0,
      /* columns config for tree table */
      columns: [
        {
          label: 'Category Name',
          prop: 'cat_name'
        },
        {
          label: 'Is Deleted',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isDeleted'
        },
        {
          label: 'Order',
          prop: 'cat_level',
          type: 'template',
          template: 'order'
        },
        {
          label: 'Operating',
          type: 'template',
          template: 'operating'
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        /* default add category is a 1 level category  */
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          {
            required: true,
            message: 'Please enter a category name',
            trigger: 'blur'
          }
        ]
      },
      parentCateList: [],
      /* cascader config object */
      cascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      /* <id></id> array of selected parent category */
      selectedkeys: []
    }
  },
  methods: {
    async getCateList() {
      const rs = await this.$http.get('categories', { params: this.queryInfo })
      if (rs.meta.status !== 200) return this.$msg.error(rs.meta.msg)
      // console.log(JSON.stringify(rs.data))
      // this.$msg.success(rs.meta.msg)
      this.cateList = rs.data.result
      this.total = rs.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(curr) {
      this.queryInfo.pagenum = curr
      this.getCateList()
    },
    showAddCateDialog() {
      /* first get parent cate list */
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    /* get parent category list(1 & 2 level) */
    async getParentCateList() {
      const rs = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (rs.meta.status !== 200) return this.$msg.error(rs.meta.msg)
      this.parentCateList = rs.data
      // console.log(this.parentCateList);
    },
    /* option has changed will event */
    parentCateChanged() {
      /* if length > 0, then parent category selected */
      if (this.selectedkeys.length > 0) {
        /* parent id(pid) exist */
        this.addCateForm.cat_pid = this.selectedkeys[
          this.selectedkeys.length - 1
        ]
        /* level = length */
        this.addCateForm.cat_level = this.selectedkeys.length
        return
      }
      /* length == 0, then add a 0 level category */
      this.addCateForm.cat_pid = this.addCateForm.cat_level = 0
    },
    addCate() {
      // console.log(this.addCateForm)
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        const rs = await this.$http.post('categories', this.addCateForm)
        if (rs.meta.status !== 201) return this.$msg.error(rs.meta.msg)
        this.$msg.success(rs.meta.msg)
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    closeAddCateDialog() {
      /* clean environment */
      this.$refs.addCateFormRef.resetFields()
      this.selectedkeys = []
      this.addCateForm.cat_level = this.addCateForm.cat_pid = 0
    }
  },
  created() {
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
